package com.alogic.tlog.handler;

import org.w3c.dom.Element;

import com.alogic.tlog.TLog;
import com.anysoft.rrm.RRModelManager;
import com.anysoft.rrm.ServiceMetrics;
import com.anysoft.stream.AbstractHandler;
import com.anysoft.util.Properties;
import com.anysoft.util.Settings;


/**
 * 服务统计
 * @author yyduan
 * 
 * @since 1.6.11.4
 * @version 1.6.12.32 [20190429] <br>
 * - 优化RRM模型
 */
public class ServiceStat extends AbstractHandler<TLog>{

	protected RRModelManager rrmm = RRModelManager.get();
	
	protected String getMetricsId(String svcId){
		return "svc.thpt:" + svcId;
	}	
	
	protected void onHandle(TLog tlog, long t) {
		
		String id = tlog.id();

		// 统计服务调用次数
		String metricsId = getMetricsId(id);
		ServiceMetrics sm = new ServiceMetrics(metricsId,id);
		sm.count(tlog.duration, !tlog.code().equals("core.ok"));
		rrmm.addModel(sm,Settings.get());

		ServiceMetrics total = new ServiceMetrics("metrics.service","/all");
		total.count(tlog.duration, !tlog.code().equals("core.ok"));
		rrmm.addModel(total,Settings.get());
	}

	protected void onFlush(long t) {
		// 没有什么可以flush的
	}

	@Override
	protected void onConfigure(Element e, Properties p) {
		// nothing to do
	}
}